﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MessageQuque.Common.Base
{
    public abstract class ConfigurationSectionBase : IConfigurationSectionHandler, IDisposable
    {

        /// <summary>
        /// IConfigurationSectionHandler接口实现
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        object System.Configuration.IConfigurationSectionHandler.Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            try
            {
                // Creates the configuration object that this method will return.
                // This can be a custom configuration class.
                // In this example, we use a System.Collections.Hashtable.
                Hashtable myConfigObject = new Hashtable();

                // Gets any attributes for this section element.
                Hashtable myAttribs = new Hashtable();
                foreach (XmlAttribute attrib in section.Attributes)
                {
                    if (XmlNodeType.Attribute == attrib.NodeType)
                        myAttribs.Add(attrib.Name, attrib.Value);
                }

                // Puts the section name and attributes as the first config object item.
                myConfigObject.Add(section.Name, myAttribs);

                // Gets the child element names and attributes.
                foreach (XmlNode child in section.ChildNodes)
                {
                    if (XmlNodeType.Element == child.NodeType)
                    {
                        Hashtable myChildAttribs = new Hashtable();

                        foreach (XmlAttribute childAttrib in child.Attributes)
                        {
                            if (XmlNodeType.Attribute == childAttrib.NodeType)
                                myChildAttribs.Add(childAttrib.Name, childAttrib.Value);
                        }
                        myConfigObject.Add(child.Name, myChildAttribs);
                    }
                }

                return (myConfigObject);
            }
            catch
            {
                return null;
            }
        }

        void IDisposable.Dispose()
        {

        }
    }
}
